<template>
  <div class="container">
    <nav-bar :title="titleName" />
    <div class="form">
      <div class="title">{{titleName}}</div>
      <!-- 手机号码注册 -->
      <div class="ipt ipt-phone">
        <div class="country-num">+86</div>
        <input type="number" v-model="phone" placeholder="手机号码" />
      </div>
      <div class="form-btns">
        <div class="form-btn" :class="{'btn-bg': passReg}" @click="registerNext">下一步</div>
      </div>
      <div @click="goToRegisterEmail" class="tips">
        <!-- 使用手机号注册 -->
        使用邮箱{{titleName}}
      </div>
    </div>
  </div>
</template>

<script>
import NavBar from 'components/common/NavBar'
import { Toast } from 'vant'
import { phoneReg } from '../../utils'
import { getPhoneCode } from '../../network/user'

export default {
  components: {
    NavBar
  },
  data() {
    return {
      userKey: localStorage.getItem('userKey'),
      phone: '',
      isForget: false,
    }
  },
  computed: {
    titleName(){
      let nameObj = {
        normal: '注册',
        bind: '绑定',
        forget: '找回密码',
      }
      return nameObj[this.$route.query.type]
    },
    passReg(){
      return phoneReg(this.phone)
    }
  },
  mounted() {
    this.isForget = this.$route.query.type === 'forget'
  },
  methods: {
    registerNext(){
      if(!this.passReg){
        Toast('手机号输入错误')
      } else {
        this.$route.query.phone = this.phone
        
        let requestData = {
          mobile: this.phone,
          isChangePwd: this.isForget,
        }
        getPhoneCode(requestData, flag => {
          if(flag){
            Toast('短信验证码已发送')
          }
          setTimeout(() => {
            this.$router.push({
              path: '/registerValidate',
              query: this.$route.query
            })
          }, 1000);
        })
      }
    },
    goToRegisterEmail(){
      this.$router.push({
        path: '/registerEmail',
        query: {
          type: this.$route.query.type
        }
      })
    }
  },
}
</script>

<style scoped>
.container{
  background: #ffffff;
}
.form{
  padding: 40px 30px;
}
.form .title{
  font-size: 22px;
  line-height: 1.6;
  padding-bottom: 20px;
}
.form .ipt{
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}
.form .ipt input{
  flex-grow: 1;
  height: 30px;
  line-height: 30px;
  font-size: 16px;
  border-bottom: 1px solid #e0e0e0;
}
.form .ipt .country-num{
  height: 30px;
  line-height: 30px;
  padding-right: 10px;
  border-bottom: 1px solid #e0e0e0;
}
.form-btn{
  font-size: 14px;
  text-align: center;
  line-height: 45px;
  color: #ffffff;
  background-color: #d1d1d1;
}
.form-btn.btn-bg{
  background-color: #000;
}
.tips{
  display: block;
  font-size: 12px;
  padding-top: 5px;
  line-height: 3.5;
  color: #0598dd;
  text-align: center;
}
</style>